package com.basic.library.view.drawableview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import com.basic.library.R;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;


/**
 * Created by YYS on 2017/12/27.
 * * 可实现大小调节图标、图标居中显示，title两行以上带左右icon的可以使图标与第一行对齐显示
 */

public class DrawableTextView extends AppCompatTextView {

    public static final int AT_THE_RATIO = -1;//按比例
    private int drawableWidth = AT_THE_RATIO;
    private int drawableHeight = AT_THE_RATIO;
    protected boolean drawableIsCenter = false;
    private boolean isAliganCenter = true;

    public DrawableTextView(Context context) {
        super(context);
        init(context, null, 0);
    }

    public DrawableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public DrawableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }


    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DrawableTextView, defStyleAttr, 0);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            if (attr == R.styleable.DrawableTextView_drawableWidth) {
                drawableWidth = a.getDimensionPixelSize(attr, 0);
            } else if (attr == R.styleable.DrawableTextView_drawableHeight) {
                drawableHeight = a.getDimensionPixelSize(attr, 0);
            } else if (attr == R.styleable.DrawableTextView_drawableIsCenter) {
                drawableIsCenter = a.getBoolean(attr, false);
            } else if (attr == R.styleable.DrawableTextView_isAliganCenter) {
                isAliganCenter = a.getBoolean(attr, true);
            }
        }
        a.recycle();
        setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[0], getCompoundDrawables()[1], getCompoundDrawables()[2], getCompoundDrawables()[3]);
    }

    public void setDrawableSize(float width, float height) {
        drawableWidth = (int) width;
        drawableHeight = (int) height;
        setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[0], getCompoundDrawables()[1], getCompoundDrawables()[2], getCompoundDrawables()[3]);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[0], getCompoundDrawables()[1], getCompoundDrawables()[2], getCompoundDrawables()[3]);
    }

    @Override
    public void setCompoundDrawablesWithIntrinsicBounds(@Nullable Drawable left, @Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom) {
        if (left != null) {
            if (drawableHeight == AT_THE_RATIO && drawableWidth == AT_THE_RATIO) {
                drawableWidth = left.getIntrinsicWidth();
                drawableHeight = left.getIntrinsicHeight();
            } else if (drawableWidth == AT_THE_RATIO) {
                drawableWidth = drawableHeight * left.getIntrinsicWidth() / left.getIntrinsicHeight();
            } else if (drawableHeight == AT_THE_RATIO) {
                drawableHeight = drawableWidth * left.getIntrinsicHeight() / left.getIntrinsicWidth();
            }
            int top1 = isAliganCenter ? 0 : -getLineCount() * getLineHeight() / 2 + getLineHeight() / 2;
            left.setBounds(0, top1, drawableWidth, top1 + drawableHeight);
        }
        if (top != null) {
            if (drawableHeight == AT_THE_RATIO && drawableWidth == AT_THE_RATIO) {
                drawableWidth = top.getIntrinsicWidth();
                drawableHeight = top.getIntrinsicHeight();
            } else if (drawableWidth == AT_THE_RATIO) {
                drawableWidth = drawableHeight * top.getIntrinsicWidth() / top.getIntrinsicHeight();
            } else if (drawableHeight == AT_THE_RATIO) {
                drawableHeight = drawableWidth * top.getIntrinsicHeight() / top.getIntrinsicWidth();
            }
            int top1 = isAliganCenter ? 0 : -getLineCount() * getLineHeight() / 2 + getLineHeight() / 2;
            top.setBounds(0, top1, drawableWidth, top1 + drawableHeight);
        }
        if (right != null) {
            if (drawableHeight == AT_THE_RATIO && drawableWidth == AT_THE_RATIO) {
                drawableWidth = right.getIntrinsicWidth();
                drawableHeight = right.getIntrinsicHeight();
            } else if (drawableWidth == AT_THE_RATIO) {
                drawableWidth = drawableHeight * right.getIntrinsicWidth() / right.getIntrinsicHeight();
            } else if (drawableHeight == AT_THE_RATIO) {
                drawableHeight = drawableWidth * right.getIntrinsicHeight() / right.getIntrinsicWidth();
            }
            int top1 = isAliganCenter ? 0 : -getLineCount() * getLineHeight() / 2 + getLineHeight() / 2;
            right.setBounds(0, top1, drawableWidth, top1 + drawableHeight);
        }

        if (bottom != null) {
            if (drawableHeight == AT_THE_RATIO && drawableWidth == AT_THE_RATIO) {
                drawableWidth = bottom.getIntrinsicWidth();
                drawableHeight = bottom.getIntrinsicHeight();
            } else if (drawableWidth == AT_THE_RATIO) {
                drawableWidth = drawableHeight * bottom.getIntrinsicWidth() / bottom.getIntrinsicHeight();
            } else if (drawableHeight == AT_THE_RATIO) {
                drawableHeight = drawableWidth * bottom.getIntrinsicHeight() / bottom.getIntrinsicWidth();
            }
            int top1 = isAliganCenter ? 0 : -getLineCount() * getLineHeight() / 2 + getLineHeight() / 2;
            bottom.setBounds(0, top1, drawableWidth, top1 + drawableHeight);
        }

        setCompoundDrawables(left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas = getTopCanvas(canvas);
        super.onDraw(canvas);
    }

    private Canvas getTopCanvas(Canvas canvas) {
        Drawable[] drawables = getCompoundDrawables();
        if (drawableIsCenter && drawables != null) {
            if (drawables[1] != null || drawables[3] != null) {//上下的drawable
                float textSize = getPaint().getTextSize();
                int drawPadding = getCompoundDrawablePadding() + 10;//TODO 这里不太对，暂时+10解决
                float contentHeight = textSize + drawableHeight + drawPadding;
                int topPadding = (int) (getHeight() - contentHeight) / 2;
                setPadding(0, topPadding, 0, topPadding);
            } else {//左右的drawable
                // float textSize = getPaint().getTextSize(); // 使用这个会导致文字竖向排下来  
                float textSize = getPaint().measureText(getText().toString());
                int drawPadding = getCompoundDrawablePadding();
                float contentWidth = textSize + drawableWidth + drawPadding;
                int leftPadding = (int) (getWidth() - contentWidth);
                setPadding(leftPadding / 2, 0, leftPadding / 2, 0);
            }
        }
        return canvas;
    }
}
